package com.yoomiito.app.address

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.adapter.address.AddressAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.bean.AddressInfo
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.NoClickLinearLayout
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.StatusManager
import kotlinx.android.synthetic.main.act_address_list.*
import kotlinx.android.synthetic.main.layout_rcy.*
import kotlinx.android.synthetic.main.title_bar.*


class AddressListActivity : BaseActivity<AddressListPresenter>() {
    private val mAdapter = AddressAdapter(null)
    private var mAddressInfo: AddressInfo? = null
    private var showCheckbox = false

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, AddressListActivity::class.java)
            intent.putExtra(Const.Common.ShowCheckBox, false)
            context.startActivity(intent)
        }

        fun startForResult(activity: BaseActivity<*>, addressInfo: AddressInfo? = null, requestCode: Int) {
            val intent = Intent(activity, AddressListActivity::class.java)
            if (addressInfo != null)
                intent.putExtra(Const.Common.Data, addressInfo)
            intent.putExtra(Const.Common.ShowCheckBox, true)
            activity.startActivityForResult(intent, requestCode)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_address_list

    override fun newP(): AddressListPresenter = AddressListPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {

        val addressInfo = intent.getSerializableExtra(Const.Common.Data)
        if (addressInfo != null)
            mAddressInfo = addressInfo as AddressInfo
        showCheckbox = intent.getBooleanExtra(Const.Common.ShowCheckBox, false)
        iv_back_left.setOnClickListener { finish() }
        title_bar.setBackgroundColor(ResUtil.getColor(R.color.color_F4F4F4))
        tv_center.text = "收货地址"
        tv_center.paint.isFakeBoldText = true
        tv_right.visibility = View.VISIBLE
        tv_right.text = "添加地址"
        tv_right.textSize = 16f
        tv_right.setTextColor(ResUtil.getColor(R.color.color_balk_333333))
        tv_right.setOnClickListener {
            EditAddressActivity.start(this, null)
        }

        mStatusManager = StatusManager.Builder(this)
                .addType(ContentType(R.layout.layout_rcy))
                .addType(EmptyType().setEmptyContent("未添加收货地址"))
                .build()
        frameLayout.addView(mStatusManager.view())
        layout_rcy.setBackgroundColor(ResUtil.getColor(R.color.color_F4F4F4))

        layout_rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        layout_rcy.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(12f), DensityUtil.dp2px(10f), false))
        layout_rcy.adapter = mAdapter
        mAdapter.setOnItemChildClickListener { adapter, view, position ->

            val addressInfo = mAdapter.data[position]
            when (view) {
                is TextView -> {//编辑
                    EditAddressActivity.start(this, addressInfo)
                }
                is NoClickLinearLayout -> {//点击选中当前地址
                    mAdapter.data.forEach {
                        if (it.select=="1")it.select = "0"
                    }
                    addressInfo.select = "1"
                    addressInfo.areaName = addressInfo.provinceName+addressInfo.cityName+addressInfo.countyName
                    val intent = Intent()
                    intent.putExtra(Constant.DATA, addressInfo)
                    setResult(0, intent)
                    finish()
                }
            }
        }
        showLoading()
        p.addressList()
    }

    fun setData(data: List<AddressInfo>?) {
        dismissLoading()
        if (data == null || data.isEmpty()) {
            showEmptyView()
        } else {
            showContentView()
            data.forEach {
                it.isShowCheckBox = showCheckbox
                it.select = if (mAddressInfo == null) it.isDefault else if (it.id == mAddressInfo?.id) "1" else "0"
            }
            mAdapter.setNewData(data)
        }
//        if (data.current_page == 1) {
//
//        }else{
//            mAdapter.addData(newData as List<AddressInfo>)
//        }
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (eventMessage?.eventType == Type.EventMsgType.UPDATE_ADDRESS) {
            p.addressList()
        }
    }

}